import store from '@/store';

/**
* @description 获取页面标签
* @param {String} pageTitle 页面Title
* @return {String} 完整页面标签
*/
const getPageTitle = (pageTitle) => {
    if (!isEmpty(pageTitle)) return `${pageTitle} - ${store.getters.appTitle}`;
    else return `${store.getters.appTitle}`;
};

// /**
//  * @description 校验指定Tag权限
//  * @param {String} moduletag 菜单Tag
//  * @param {String} usecasetag 用例Tag
//  * @returns {Boolean} 是否有权限
//  */
// const checkPermission = (moduletag, usecasetag) => {
//     // if (moduletag && moduletag instanceof String && moduletag !== '' &&
//     //   usecasetag && usecasetag instanceof String && usecasetag !== '')
//     if (moduletag && typeof moduletag === 'string' && moduletag !== '' &&
//         usecasetag && typeof usecasetag === 'string' && usecasetag !== '') {
//         const hasUsecases = store.getters && store.getters.usecases;
//         const hasPermission = hasUsecases.includes(`${moduletag}_${usecasetag}`);
//         if (!hasPermission) return false;
//         return true;
//     } else return false;
// };
/**
 * @description 校验指定Tag权限
 * @param {String} usecasetag 用例Tag
 * @returns {Boolean} 是否有权限
 * @example src\views\system\orginfo\index.vue
 */
const checkPermission = (usecasetag) => {
    if (usecasetag && typeof usecasetag === 'string' && usecasetag !== '') {
        const hasUsecases = store.getters && store.getters.usecases;
        const hasPermission = hasUsecases.includes(usecasetag);
        if (!hasPermission) return false;
        return true;
    } else return false;
};

/**
* @description 验证对象是否为空
* @param {Object} obj=[Object|String|Number] 待验证对象
* @return {Boolean} 是否为空对象
*/
const isEmpty = function (obj) {
    if (obj == null ||
        typeof obj === "undefined" ||
        new RegExp(/^[\s]*$/).test(obj) ||
        JSON.stringify(obj) === "{}") {
        return true;
    } else return false;
    // obj.replace(/(^s*)|(s*$)/g, "")
    // return new RegExp("^[ ]+$").test(str);
};

/**
* @description 过滤对象中空值
* @param {Object} obj=[Object|String|Array] 待过滤对象
* @return {Object} 过滤后对象
*/
const filterNull = (obj) => {
    for (var key in obj) {
        if (obj[key] === null) {
            delete obj[key];
            return;
        }
        if (toType(obj[key]) === "string") {
            obj[key] = obj[key].trim();
        } else if (toType(obj[key]) === "object") {
            obj[key] = filterNull(obj[key]);
        } else if (toType(obj[key]) === "array") {
            obj[key] = filterNull(obj[key]);
        }
    }
    return obj;
};
/**
* @description 字符串转小写
* @param {Object} obj 待转化对象
* @return {Object} 转化后字符串
*/
const toType = (obj) => {
    return {}.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
};

/**
 * @description Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
const parseTime = (time, cFormat) => {
    // if (arguments.length === 0) { return null; }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
    let date;
    if (typeof time === 'object') date = time;
    else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time);
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000;
        }
        date = new Date(time);
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    };
    const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
        const value = formatObj[key];
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value]; }
        return value.toString().padStart(2, '0');
    });
    return time_str;
};

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
const formatTime = (time, option) => {
    if (('' + time).length === 10) time = parseInt(time) * 1000;
    else { time = +time; }
    const d = new Date(time);
    const now = Date.now();
    const diff = (now - d) / 1000;
    if (diff < 30) {
        return '刚刚';
    } else if (diff < 3600) {
        return Math.ceil(diff / 60) + '分钟前';
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前';
    } else if (diff < 3600 * 24 * 2) { return '1天前'; }
    if (option) return parseTime(time, option);
    else return `${d.getMonth() + 1}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分`;
};

/**
 * @param {*} str
 * @param {*} char
 * @param {*} type
 * @returns
 */
const trim = (str, char, type) => {
    if (char) {
        if (type === 'left') {
            return str.replace(new RegExp('^\\' + char + '+', 'g'), '');
        } else if (type === 'right') {
            return str.replace(new RegExp('\\' + char + '+$', 'g'), '');
        }
        return str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
    }
    return str.replace(/^\s+|\s+$/g, '');
};

export default {
    getPageTitle,
    checkPermission,
    isEmpty,
    filterNull,
    parseTime,
    formatTime,
    trim
};